package com.csc.contact.checkbill.service;

import com.csc.contact.checkbill.entity.UrpChargeinfo;
import com.csc.contact.checkbill.mapper.UrpChargeinfoMapper;
import com.hollycrm.hollybeacons.system.db.mybatis.support.BaseServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 *
 * @auther zhanglei
 * @date 2019/6/18 14:54
 * Description: 充值缴费流程
 */
@Service("urpchargeinfoService")
public class UrpChargeinfoServiceImpl extends BaseServiceImpl<UrpChargeinfoMapper, UrpChargeinfo> {

    private final static Logger LOGGER = Logger.getLogger(UrpChargeinfoServiceImpl.class);

    @Value("${client.errcount}")
    private String errCount;

    public UrpChargeinfo getChargeInfoByCgiSerialNo(String cgiSerialNo) {
        return this.baseMapper.chargeInfoQueryByCgiSerialNo(cgiSerialNo);
    }

    public List<UrpChargeinfo> chargeInfoQueryByBillDate(String billDate){
        return this.baseMapper.chargeInfoQueryByBillDate(billDate);
    }
    public String getErrCount() {
        return errCount;
    }

    public void setErrCount(String errCount) {
        this.errCount = errCount;
    }

    public int queryChargeCount(String cutofDay) {
        return this.baseMapper.queryChargeCount(cutofDay);
    }

    /**
     * 与枢纽对账中处理结果为失败的更新为成功
     *
     * @param params
     */
    public void updateChargeStatus(Map params) {
        this.baseMapper.updateChargeStatus(params);
    }

    /**
     * 更新枢纽未传缴费记录，将状态不是成功的改为失败
     *
     * @param params
     */
    public void updateChargeFailure(Map params) {
        this.baseMapper.updateChargeFailure(params);
    }

    /**
     * 查询枢纽未传的缴费记录，且状态非成功的流水号
     *
     * @param params
     */
    public List<UrpChargeinfo> queryChargeFailure(Map params) {
        return this.baseMapper.queryChargeFailure(params);
    }

    /**
     * 枢纽无，缴费有，且缴费表状态为0成功，将其改为1失败
     *
     * @param params
     */
    public List<UrpChargeinfo> queryChargeZero(Map params) {
        return this.baseMapper.queryChargeZero(params);
    }


    /**
     * 更新逻辑日
     * @param params
     */
    public void updateCheckDay(Map params) {
        this.baseMapper.updateCheckDay(params);
    }


    /**
     * 获取逻辑日中单一省份充值缴费成功的总记录数
     *
     * @param params
     * @return
     */
    public int queryCountByProvince(Map params) {
        try {
            return this.baseMapper.queryCountByProvince(params);
        } catch (Exception e) {
            LOGGER.info("获取逻辑日中单一省份充值缴费成功的总记录数出错====", e);
            return 0;
        }
    }

    /**
     * 获取逻辑日中单一省份成功充值缴费的总金额
     *
     * @param params
     * @return
     */

    public int querySumMoneyByProvince(Map params) {
        try {
            return this.baseMapper.querySumMoneyByProvince(params) == null ? 0 : this.baseMapper.querySumMoneyByProvince(params);
        } catch (Exception e) {
            LOGGER.info("获取逻辑日中单一省份成功充值缴费的总金额出错=====", e);
            return 0;
        }
    }


    /**
     * 根据发起地省份编码获取充值缴费记录表中成功的记录，写入文件返回给号卡
     *
     * @param params
     * @return
     */
    public List<UrpChargeinfo> queryChargeInfoByEntryProvince(Map params) {
        try {
            return this.baseMapper.queryChargeInfoByEntryProvince(params);
        } catch (Exception e) {
            LOGGER.info("根据发起地省份编码获取充值缴费记录表中成功的记录异常=====", e);
            return new ArrayList<>();
        }
    }

    /**
     * 插入缴费记录
     */
    public void insertChargeinfo(UrpChargeinfo urpChargeinfo){
        this.baseMapper.saveChargeinfo(urpChargeinfo);
    }
}
